package com.shengzai.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

public class SingleColumnValueFilterData {
    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("api_test:api_student");

        Table table = conn.getTable(tableName);
        /**

         family – name of column family
         qualifier – name of column qualifier
         compareOp – operator
         value – value to compare column values against

         */
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                "students_info".getBytes(),
                "name".getBytes(),
                CompareFilter.CompareOp.EQUAL,
                "池若松".getBytes()
        );

        Scan scan = new Scan();

        scan.setFilter(singleColumnValueFilter);

        ResultScanner scanner = table.getScanner(scan);

        Result result;

        while ((result=scanner.next())!=null){
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String attribute = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(rowKey+"\t"+family+"\t"+attribute+"\t"+value);
            }
        }


    }
}
